package cn.lichenfei.javapackage.controller;

import cn.hutool.core.date.ChineseDate;
import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.util.Duration;

import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;

public class ExampleController implements Initializable {

    @FXML
    private Label cfLabel;

    @FXML
    private Label dateLabel;

    //移动动画
    private TranslateTransition t1 = new TranslateTransition(Duration.millis(500));
    //透明度动画
    private FadeTransition t2 = new FadeTransition(Duration.millis(500));
    //
    private ParallelTransition pt = new ParallelTransition(t1, t2);

    @FXML
    private void show(ActionEvent actionEvent) {
        if (1 == cfLabel.getOpacity()) {
            return;
        }
        pt.stop();//先停止
        t1.setFromY(-100);
        t1.setToY(0);
        t1.setFromX(-100);
        t1.setToX(0);
        t2.setFromValue(0);
        t2.setToValue(1);
        pt.play();
        pt.setOnFinished(event -> cfLabel.setOpacity(1));
    }

    @FXML
    private void hide(ActionEvent actionEvent) {
        if (0 == cfLabel.getOpacity()) {
            return;
        }
        pt.stop();//先停止
        t1.setFromY(0);
        t1.setToY(-100);
        t1.setFromX(0);
        t1.setToX(100);
        t2.setFromValue(1);
        t2.setToValue(0);
        pt.play();
        pt.setOnFinished(event -> cfLabel.setOpacity(0));
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        t1.setNode(cfLabel);
        t2.setNode(cfLabel);
        cfLabel.setOpacity(0);
        ChineseDate chineseDate = new ChineseDate(LocalDate.now());
        dateLabel.setText(chineseDate.getCyclicalYMD() + " - " + chineseDate);
    }
}